﻿using com.nm.ApiServer;
using com.nm.ApiServer.Attributes;
using com.nm.Attributes;
using com.nm.db.Models.Terminal;
using EntityFramework.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TerminalAPI.models;

namespace TerminalAPI
{
    [Description("省份与企划区域对照表")]
    public class CFARelationsHandler:BaseHandler
    {
        [JsonType]
        [MustLogin]
        [Description("设置省份与企划区域关系")]
        [ReturnType(typeof(int),"0-成功")]
        public ApiResult SetUp(AreaListParam param)
        {
            if (param == null) return Error("参数对象为空");
            if (param.City == null || param.City.Count == 0) return Error("省份列表为空");
            var dc = new TerminalEntities();
            dc.t_cityforarea.Where(t=>t.dictDataValue1==param.AreaID).Delete();
            foreach (var item in param.City)
            {
                var city = new t_cityforarea
                {
                    AreaID = item,
                    dictDataValue1 = param.AreaID
                };
                dc.t_cityforarea.Add(city);
            }
            try
            {
                dc.SaveChanges();
            }
            catch(Exception ex)
            {
                return Error(ex.Message);
            }
            return Success(0);
        }

        [MustLogin]
        [Description("根据企划区域获取设置的城市")]
        [ReturnType(typeof(IList<sys_v_areas>))]
        public ApiResult GetList([ParamDescription("企划区划ID")] string AreaID)
        {
            var dc = new TerminalEntities();
            var query = (from area in dc.sys_v_areas
                         join city in dc.t_cityforarea on area.AreaID equals city.AreaID
                         into temp  from tt in temp.DefaultIfEmpty()
                         where tt.dictDataValue1==AreaID
                         select area);
            var result = query.ToList().Select(o => new sys_v_areas
            {
                AID = o.AID,
                AllowUsed = o.AllowUsed,
                AreaCode = o.AreaCode,
                AreaID = o.AreaID,
                AreaName = o.AreaName,
                CheckState = o.CheckState,
                FullName = o.FullName,
                FullParentID = o.FullParentID,
                IsDetail = o.IsDetail,
                Level = o.Level,
                ModifyDTM = o.ModifyDTM,
                ParentID = o.ParentID
            }).ToList();
            return Success(result);
        }

        [MustLogin]
        [Description("获取城市列表")]
        [ReturnType(typeof(IList<CFAModel>))]
        public ApiResult CityList()
        {
            var dc = new TerminalEntities();
            var query = (from city in dc.sys_v_areas
                         where city.Level==1
                         select new
                         {
                             city,isChoose=dc.t_cityforarea.Any(t=>t.AreaID==city.AreaID)
                         });
            var result = query.ToList().Select(o => new CFAModel
            {
                AID = o.city.AID,
                AllowUsed = o.city.AllowUsed,
                AreaCode = o.city.AreaCode,
                AreaID = o.city.AreaID,
                AreaName = o.city.AreaName,
                CheckState = o.city.CheckState,
                FullName = o.city.FullName,
                FullParentID = o.city.FullParentID,
                IsDetail = o.city.IsDetail,
                Level = o.city.Level,
                ModifyDTM = o.city.ModifyDTM,
                ParentID = o.city.ParentID,
                IsChoose=o.isChoose
            }).ToList();
            return Success(result);
        }
        
    }
    public class AreaListParam
    {
        [ParamDescription("企划区域ID")]
        public string AreaID { get; set; }

        [ParamDescription("城市列表")]
        public IList<string> City { get; set; }
    }
}
